﻿namespace KeRui.DAL
{
    using KeRui.DBUtility;
    using KeRui.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class CmsTemplateFile
    {
        public int Add(KeRui.Model.CmsTemplateFile model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@TemplateID", SqlDbType.Int, 4), new SqlParameter("@FileName", SqlDbType.VarChar, 50), new SqlParameter("@FileContent", SqlDbType.NText), new SqlParameter("@FileType", SqlDbType.Int, 4), new SqlParameter("@FileUpdateInterval", SqlDbType.Int, 4), new SqlParameter("@IsDefault", SqlDbType.Int, 4), new SqlParameter("@IsTop", SqlDbType.Int, 4) };
            parameters[0].Value = model.TemplateID;
            parameters[1].Value = model.FileName;
            parameters[2].Value = model.FileContent;
            parameters[3].Value = model.FileType;
            parameters[4].Value = model.FileUpdateInterval;
            parameters[5].Value = model.IsDefault;
            parameters[6].Value = model.IsTop;
            return DbHelperSQL.RunProcedure("CmsTemplateFile_ADD", parameters, out num);
        }

        public void Delete(int FileID)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@FileID", SqlDbType.Int, 4) };
            parameters[0].Value = FileID;
            DbHelperSQL.RunProcedure("CmsTemplateFile_Delete", parameters, out num);
        }

        public KeRui.Model.CmsTemplateFile GetModel(int FileID)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@FileID", SqlDbType.Int, 4) };
            parameters[0].Value = FileID;
            KeRui.Model.CmsTemplateFile file = new KeRui.Model.CmsTemplateFile();
            DataSet set = DbHelperSQL.RunProcedure("CmsTemplateFile_GetModel", parameters, "ds");
            if (set.Tables[0].Rows.Count > 0)
            {
                if (set.Tables[0].Rows[0]["FileID"].ToString() != "")
                {
                    file.FileID = int.Parse(set.Tables[0].Rows[0]["FileID"].ToString());
                }
                if (set.Tables[0].Rows[0]["TemplateID"].ToString() != "")
                {
                    file.TemplateID = int.Parse(set.Tables[0].Rows[0]["TemplateID"].ToString());
                }
                file.FileName = set.Tables[0].Rows[0]["FileName"].ToString();
                file.FilePath = set.Tables[0].Rows[0]["FilePath"].ToString();
                file.FileContent = set.Tables[0].Rows[0]["FileContent"].ToString();
                if (set.Tables[0].Rows[0]["FileType"].ToString() != "")
                {
                    file.FileType = int.Parse(set.Tables[0].Rows[0]["FileType"].ToString());
                }
                if (set.Tables[0].Rows[0]["FileUpdateInterval"].ToString() != "")
                {
                    file.FileUpdateInterval = int.Parse(set.Tables[0].Rows[0]["FileUpdateInterval"].ToString());
                }
                if (set.Tables[0].Rows[0]["FileUpdateTime"].ToString() != "")
                {
                    file.FileUpdateTime = DateTime.Parse(set.Tables[0].Rows[0]["FileUpdateTime"].ToString());
                }
                if (set.Tables[0].Rows[0]["UsingNum"].ToString() != "")
                {
                    file.UsingNum = int.Parse(set.Tables[0].Rows[0]["UsingNum"].ToString());
                }
                if (set.Tables[0].Rows[0]["CreateTime"].ToString() != "")
                {
                    file.CreateTime = DateTime.Parse(set.Tables[0].Rows[0]["CreateTime"].ToString());
                }
                if (set.Tables[0].Rows[0]["UpdateTime"].ToString() != "")
                {
                    file.UpdateTime = DateTime.Parse(set.Tables[0].Rows[0]["UpdateTime"].ToString());
                }
                if (set.Tables[0].Rows[0]["IsTop"].ToString() != "")
                {
                    file.IsTop = int.Parse(set.Tables[0].Rows[0]["IsTop"].ToString());
                }
                if (set.Tables[0].Rows[0]["IsDefault"].ToString() != "")
                {
                    file.IsDefault = int.Parse(set.Tables[0].Rows[0]["IsDefault"].ToString());
                }
                return file;
            }
            return null;
        }

        public void Reduce(KeRui.Model.CmsTemplateFile model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@FileID", SqlDbType.Int, 4), new SqlParameter("@FileContent", SqlDbType.NText) };
            parameters[0].Value = model.FileID;
            parameters[1].Value = model.FileContent;
            DbHelperSQL.RunProcedure("CmsTemplateFile_Reduce", parameters, out num);
        }

        public int Update(KeRui.Model.CmsTemplateFile model)
        {
            int num;
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@FileID", SqlDbType.Int, 4), new SqlParameter("@FileContent", SqlDbType.NText), new SqlParameter("@FileUpdateInterval", SqlDbType.Int, 4), new SqlParameter("@IsTop", SqlDbType.Int, 4), new SqlParameter("@FileType", SqlDbType.Int, 4) };
            parameters[0].Value = model.FileID;
            parameters[1].Value = model.FileContent;
            parameters[2].Value = model.FileUpdateInterval;
            parameters[3].Value = model.IsTop;
            parameters[4].Value = model.FileType;
            return DbHelperSQL.RunProcedure("CmsTemplateFile_Update", parameters, out num);
        }
    }
}

